package com.ponxu.web.core;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.json.simple.JSONObject;

import com.ponxu.utils.Lang;

/**
 * request, response实用封装
 * 
 * @author xwz
 * 
 */
public class WebContext {
	private HttpServletRequest request;
	private HttpServletResponse response;

	public WebContext(HttpServletRequest request, HttpServletResponse response) {
		this.request = request;
		this.response = response;
	}

	public HttpServletRequest Request() {
		return request;
	}

	public HttpServletResponse Response() {
		return response;
	}

	// =======================================================

	public String getParameter(String name) {
		return Lang.defaultString(request.getParameter(name));
	}

	public int getIntParameter(String name) {
		return Integer.parseInt(Lang.defaultString(getParameter(name), "0"));
	}

	public Cookie cookieGet1(String name) {
		Cookie[] cookies = request.getCookies();
		if (cookies == null)
			return null;
		for (Cookie cookie : cookies) {
			if (cookie.getName().equalsIgnoreCase(name))
				return cookie;
		}
		return null;
	}

	public String cookieGet(String name) {
		Cookie cookie = cookieGet1(name);
		return cookie == null ? null : cookie.getValue();
	}

	public void cookieSet(String name, String value, String path, int time) {
		Cookie cookie = new Cookie(name, value);
		cookie.setPath(path);
		cookie.setMaxAge(time);
		response.addCookie(cookie);
	}

	public void cookieRemove(String name) {
		Cookie cookie = cookieGet1(name);
		if (cookie != null) {
			cookie.setMaxAge(0);
			response.addCookie(cookie);
		}
	}

	// =======================================================

	public void forward(String url) throws ServletException, IOException {
		request.getRequestDispatcher(url).forward(request, response);
	}

	public void redirect(String url) throws IOException {
		response.sendRedirect(url);
	}

	// =======================================================
	public void write(String str) throws IOException {
		response.getWriter().write(str);
	}

	public void writeJSON(JSONObject obj) throws IOException {
		response.setContentType("application/json; charset=UTF-8");
		write(obj.toJSONString());
	}

	// =======================================================

	public Object sessionGet(String name) {
		HttpSession session = request.getSession(false);
		return (session != null ? session.getAttribute(name) : null);
	}

	public void sessionSet(String name, Object value) {
		HttpSession session = request.getSession(true);
		if (session != null) {
			session.setAttribute(name, value);
		}
	}

	public void sessionRemove(String name) {
		HttpSession session = request.getSession(false);
		if (session != null) {
			session.removeAttribute(name);
		}
	}

	public String getHeader(String name) {
		return request.getHeader(name);
	}

	public void put(String name, Object val) {
		request.setAttribute(name, val);
	}
}
